/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package fr.ove.openmath.mathematica;

/**
 *
 * @author Mr.Sam
 */
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

/**
* A splash screen.
*/
class SplashScreen extends JWindow {
    public SplashScreen(String filename, Frame f, int waitTime) {
        super(f);
        JLabel l = new JLabel(new ImageIcon(filename));
        getContentPane().add(l, BorderLayout.CENTER);
        pack();
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        Dimension labelSize = l.getPreferredSize();
        setLocation(screenSize.width/2 - (labelSize.width/2),
                    screenSize.height/2 - (labelSize.height/2));
        addMouseListener(
            new MouseAdapter() {
                public void mousePressed(MouseEvent e)
                {
                    setVisible(false);
                    dispose();
                }
            }
        );
        
        final Runnable closerRunner = new Runnable() {
            public void run() {
                setVisible(false);
                dispose();
            }
        };
         
        final int pause = waitTime;
        Runnable waitRunner = new Runnable() {
            public void run() {
                try {
                        Thread.sleep(pause);
                        SwingUtilities.invokeAndWait(closerRunner);
                }
                catch(Exception e) {
                        e.printStackTrace();
                        // can catch InvocationTargetException
                        // can catch InterruptedException
                }
            }
        };
        
        setVisible(true);
        Thread splashThread = new Thread(waitRunner, "SplashThread");
        splashThread.start();
    }
}